package com.topfinance.cnaps2.web.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.topfinance.cnaps2.dto.ConfOutPortDto;
import com.topfinance.cnaps2.ebo.ConfOutPortEbo;
import com.topfinance.cnaps2.service.IOutPortManager;
import com.topfinance.frame.util.StringUtil;
import com.topfinance.frame.web.BaseSupport;
import com.topfinance.payment.dto.TCfgOutPortDto;
import com.topfinance.payment.ebo.TCfgPortOutEbo;
import com.topfinance.payment.service.ITCfgPortOutMgr;
import com.opensymphony.xwork2.ModelDriven;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author
 * @version 1.50
 *          <p>
 *          Copyright: Copyright (c) 2004 2005
 *          </p>
 *          <p>
 *          Company:
 *          </p>
 */
public final class CfgPortOutAction extends BaseSupport implements ModelDriven<TCfgOutPortDto> {

	private ITCfgPortOutMgr portManager = null;

	private TCfgOutPortDto portDto = new TCfgOutPortDto();

	public TCfgOutPortDto getModel() {
		return portDto;
	}

	public void setCfgPortOutMgr(ITCfgPortOutMgr portManager) {
		this.portManager = portManager;
	}

	public CfgPortOutAction() {
		super();
	}

	public String execute() {
		return list();
	}

	public String list() {
		log.debug("CfgTransAction-list begin");
		try {
			List<TCfgPortOutEbo> l = portManager.getAllPortOut();
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("CfgTransAction-list end");
		return SUCCESS;
	}

	public String create() {
		try {
			log.debug(StringUtil.mapObject(portDto));
			TCfgPortOutEbo o = portManager.addOutPort(portDto);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "" + o.getOid();
			rtnResultMap.put("info", getText(CREATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String update() {
		try {
			log.debug(StringUtil.mapObject(portDto));
			portManager.editOutPort(portDto);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(UPDATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}
	
	public String delete() {
		try {
			String delId = request.getParameter("id");
			portManager.deleteOutPort(new Integer(delId));
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(DELETE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}	

	public TCfgPortOutEbo getCfgOutPortEbo() {
		return portDto;
	}

	public void setCfgOutPortInfoEbo(TCfgOutPortDto portDto) {
		this.portDto = portDto;
	}

	public String query() {
		if (request.getParameter("id") == null) {
			rtnResultMap = new HashMap();
			String[] args = new String[1];
			args[0] = "";// you can add extra message here
			rtnResultMap.put("info", getText("expect.valid.pk.data", args));
			return SUCCESS;
		}
		try {
			String uid = request.getParameter("id");
			TCfgPortOutEbo trans = portManager.getOutPort(new Integer(uid));
			List l = new ArrayList<ConfOutPortEbo>();
			l.add(trans);
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, 1);
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}	
	
	public String test() {
		log.debug("SysFuncAction-test begin");
//		try {
//			ConfOutPortDto pDto = new ConfOutPortDto();
//			pDto.setUid(1003);
//			pDto.setName("测试端口2");
//			pDto.setDirection("D");
//			//pDto.setType("O");
//			//pDto.setAppendix("附属说明2222");
//			//pDto.setInOut("IN");
//			
//			pDto.setNID(1020);//
//			pDto.setTID(1020);
//			portManager.addOutPort(pDto);
//			rtnResultMap = new HashMap();
//			rtnResultMap.put("info", getText("create.success", "Test Success!"));
//			return SUCCESS;
//		} catch (Exception e) {
//			handleException(e);
//		}
		log.debug("SysFuncAction-list end");
		return SUCCESS;
	}
	
	
}
